import { defineStore } from 'pinia'
import { Notification, NotificationState, AddNotificationParams } from '@/types/notification'

export const useNotificationStore = defineStore('notification', {
  state: (): NotificationState => ({
    notifications: [
      {
        id: 1,
        title: '系统通知',
        content: '您的账号已开通全部权限',
        time: '2023-12-01 10:30',
        isRead: false,
        type: 'success'
      },
      {
        id: 2,
        title: '待办事项',
        content: '您有3个任务待处理',
        time: '2023-12-02 14:25',
        isRead: false,
        type: 'info'
      },
      {
        id: 3,
        title: '消息提醒',
        content: '库存不足提醒：产品A库存低于警戒值',
        time: '2023-12-03 09:15',
        isRead: false,
        type: 'warning',
        link: '/inventory/products'
      }
    ],
    lastId: 3
  }),
  
  getters: {
    unreadCount: (state): number => state.notifications.filter(n => !n.isRead).length,
    getNotificationById: (state) => (id: number): Notification | undefined => state.notifications.find(n => n.id === id)
  },
  
  actions: {
    markAsRead(id: number): void {
      const notification = this.notifications.find(n => n.id === id)
      if (notification) {
        notification.isRead = true
      }
    },
    
    markAllAsRead(): void {
      this.notifications.forEach(notification => {
        notification.isRead = true
      })
    },
    
    removeNotification(id: number): void {
      const index = this.notifications.findIndex(n => n.id === id)
      if (index !== -1) {
        this.notifications.splice(index, 1)
      }
    },
    
    addNotification(notification: AddNotificationParams): void {
      this.lastId++
      this.notifications.unshift({
        id: this.lastId,
        time: new Date().toLocaleString(),
        isRead: false,
        ...notification
      })
    },
    
    clearAll(): void {
      this.notifications = []
    }
  }
}) 